<?php

namespace App\Http\Controllers\Admin\Content;

use App\Enums\ResponseEnum;
use App\Http\Controllers\AdminController;
use App\Models\Content\Files;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FilesController extends AdminController
{
    public function data(Request $request)
    {
        $model = $request->get('recycle') ? Files::onlyTrashed() : Files::query();
        if ($groupId = $request->get('groupId')) {
            $model->where('group_id', (int)$groupId);
        }
        if ($keywords = $request->get('keywords')) {
            $model->where('file_name', 'like', $keywords.'%');
        }
        if ($storageMode = $request->get('storageMode')) {
            $model->where('storage', $storageMode);
        }
        if ($minDate = $request->get('minDate')) {
            $model->whereDate('created_at', '>=', $minDate);
        }
        if ($maxDate = $request->get('maxDate')) {
            $model->whereDate('created_at', '<=', $maxDate);
        }
        $list = $model->select(['id', 'file_name', 'path', 'suffix', 'type', 'storage', 'size', 'created_at'])
            ->orderByDesc('id')->paginate($request->get('limit', 10));
        return $this->succeed([
            'total' => $list->total(),
            'rows'  => $list->items(),
        ]);
    }

    public function download(Request $request)
    {
        $request->validate(['pathToFile' => 'required']);
        return $this->libFileSystem()->download($request->get("pathToFile"));
    }

    public function destroy(Request $request)
    {
        $request->validate([
            'ids' => ['required','array','max:'.static::QUERY_LIMIT]
        ]);
        $ids = $request->get('ids');
        DB::transaction(function () use ($ids) {
            $rows = Files::query()->whereIn('id', $ids)->limit(static::QUERY_LIMIT)->delete();
            $this->createSyslog('批量删除了'.$rows.'条附件记录', ['ids' => implode(',', $ids)]);
        });
        return $this->succeed();
    }

    public function recover(Request $request)
    {
        $request->validate([
            'ids' => ['required','array','max:'.static::QUERY_LIMIT]
        ]);
        $ids = $request->get('ids');
        DB::transaction(function () use ($ids) {
            $rows = Files::onlyTrashed()->whereIn('id', $ids)->limit(static::QUERY_LIMIT)->restore();
            $this->createSyslog('批量恢复了'.$rows.'条附件记录', ['ids' => implode(',', $ids)]);
        });
        return $this->succeed();
    }

    public function expurgate(Request $request)
    {
        $request->validate([
            'ids' => ['required','array','max:'.static::QUERY_LIMIT]
        ]);
        $ids = $request->get('ids');
        $rows = Files::onlyTrashed()->select(['id', 'path', 'storage'])->whereIn('id', $ids)->limit(static::QUERY_LIMIT)->get();
        abort_if($rows->isEmpty(), ResponseEnum::DELETE_NOT_EXIST_ERROR);
        DB::transaction(function () use ($rows) {
            foreach ($rows as $row) {
                //删除储存文件
                $this->libFileSystem($row->storage)->delete($row->path);
                $row->forceDelete();
                $this->createSyslog('彻底删除了一条附件记录', [], $row);
            }
        });
        return $this->succeed();
    }
}
